home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / wschesb1.zip / SRC / INITIALI.C < prev    next >
C/C++ Source or Header  |  1994-03-15  |  14KB  |  497 lines

  1. /*
  2.   C source for Winsock Chess
  3.   
  4.   Revision 1994-03-15
  5.   Modified by Donald Munro for use as a 2 player chess game over a 
  6.   WINSOCK layer on a TCP (or other WinSock supporting) network.
  7.   Source code and make files for MS Visual C/C++ V1.00/1.50.
  8.   February/March 1994
  9.   All GNU copyright and distribution conditions as described below and in the
  10.   file COPYING also apply to WinSock Chess.
  11.   This module is adapted from GNU Chess.
  12.   
  13.   C source for GNU CHESS
  14.  
  15.   Revision: 1990-09-30
  16.  
  17.   Modified by Daryl Baker for use in MS WINDOWS environment
  18.  
  19.   Modified by Donald Munro for use as a 2 player chess game over a 
  20.   communications link viz WINSOCK or other.
  21.   Source code and make files for MS Visual C++ V1.00/1.50
  22.   Conversion to usage of message crackers for easy conversion to win32
  23.   February/March 1994
  24.  
  25.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  26.   Copyright (c) 1988, 1989, 1990  John Stanback
  27.  
  28.   This file is part of CHESS.
  29.  
  30.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  31.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  32.   the consequences of using it or for whether it serves any particular
  33.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  34.   General Public License for full details.
  35.  
  36.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  37.   only under the conditions described in the CHESS General Public License.
  38.   A copy of this license is supposed to have been given to you along with
  39.   CHESS so you can know your rights and responsibilities.  It should be in a
  40.   file named COPYING.  Among other things, the copyright notice and this
  41.   notice must be preserved on all copies.
  42. */
  43.  
  44. #define NOATOM 
  45. #define NOCLIPBOARD
  46. #define NOCREATESTRUCT
  47. #define NOFONT
  48. #define NOREGION
  49. #define NOSOUND
  50. #define NOWH
  51. #define NOWINOFFSETS
  52. #define NOCOMM
  53. #define NOKANJI
  54.  
  55. #include <windows.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <time.h>
  59.  
  60. #include "gnuchess.h"
  61. #include "defs.h"
  62.  
  63. extern HWND hStats;
  64.  
  65. /*extern short distdata[64][64], taxidata[64][64];*/
  66. extern short far *distdata, far *taxidata;
  67.  
  68. extern FILE *hashfile;
  69. extern short stage, stage2, Developed[2];
  70. extern short ChkFlag[maxdepth], CptrFlag[maxdepth], PawnThreat[maxdepth];
  71. extern short Pscore[maxdepth], Tscore[maxdepth];
  72. extern short rehash;
  73.  
  74. /* extern struct hashval hashcode[2][7][64]; */
  75. extern struct hashval far *hashcode;
  76. extern unsigned char far * history;
  77.  
  78. void
  79. Initialize_dist (void)
  80. {
  81.   register short a, b, d, di;
  82.  
  83.   for (a = 0; a < 64; a++)
  84.     for (b = 0; b < 64; b++)
  85.       {
  86.         d = abs (column (a) - column (b));
  87.         di = abs (row (a) - row (b));
  88. /*
  89.         taxidata[a][b] = d + di;
  90.         distdata[a][b] = (d > di ? d : di);
  91. */
  92.         *(taxidata+a*64+b) = d + di;
  93.         *(distdata+a*64+b) = (d > di ? d : di);
  94.  
  95.       }
  96. }
  97.  
  98. static short _based(_segname("_CODE")) Stboard[64] =
  99. {rook, knight, bishop, queen, king, bishop, knight, rook,
  100.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  101.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  102.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  103.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  104.  rook, knight, bishop, queen, king, bishop, knight, rook};
  105.  
  106. static short _based(_segname("_CODE")) Stcolor[64] =
  107. {white, white, white, white, white, white, white, white,
  108.  white, white, white, white, white, white, white, white,
  109.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  110.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  111.  black, black, black, black, black, black, black, black,
  112.  black, black, black, black, black, black, black, black};
  113.  
  114. extern short board[64], color[64];
  115.  
  116. /*extern unsigned char nextpos[8][64][64];*/
  117. /*extern unsigned char nextdir[8][64][64];*/
  118. extern unsigned char far * nextpos;
  119. extern unsigned char far * nextdir;
  120. /*
  121.   ptype is used to separate white and black pawns, like this;
  122.   ptyp = ptype[side][piece]
  123.   piece can be used directly in nextpos/nextdir when generating moves
  124.   for pieces that are not black pawns.
  125. */
  126. static short _based(_segname("_CODE")) ptype[2][8] =
  127. {
  128.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  129.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  130.  
  131. static short _based(_segname("_CODE")) direc[8][8] =
  132. {
  133.   0, 0, 0, 0, 0, 0, 0, 0,
  134.   10, 9, 11, 0, 0, 0, 0, 0,
  135.   8, -8, 12, -12, 19, -19, 21, -21,
  136.   9, 11, -9, -11, 0, 0, 0, 0,
  137.   1, 10, -1, -10, 0, 0, 0, 0,
  138.   1, 10, -1, -10, 9, 11, -9, -11,
  139.   1, 10, -1, -10, 9, 11, -9, -11,
  140.   -10, -9, -11, 0, 0, 0, 0, 0};
  141.  
  142. static short _based(_segname("_CODE")) max_steps[8] =
  143. {0, 2, 1, 7, 7, 7, 1, 2};
  144.  
  145. static short _based(_segname("_CODE")) nunmap[120] =
  146. {
  147.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  148.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  149.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  150.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  151.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  152.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  153.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  154.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  155.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  156.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  157.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  158.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  159.  
  160.  
  161. void
  162. Initialize_moves (void)
  163.  
  164. /*
  165.   This procedure pre-calculates all moves for every piece from every square.
  166.   This data is stored in nextpos/nextdir and used later in the move generation
  167.   routines.
  168. */
  169.  
  170. {
  171.   short ptyp, po, p0, d, di, s, delta;
  172.   unsigned char far *ppos, far *pdir;
  173.   short dest[8][8];
  174.   short steps[8];
  175.   short sorted[8];
  176.  
  177.   for (ptyp = 0; ptyp < 8; ptyp++)
  178.     for (po = 0; po < 64; po++)
  179.       for (p0 = 0; p0 < 64; p0++)
  180.         {
  181.           *(nextpos+ptyp*64*64+po*64+p0) = (unsigned char) po;
  182.           *(nextdir+ptyp*64*64+po*64+p0) = (unsigned char) po;
  183.         }
  184.   for (ptyp = 1; ptyp < 8; ptyp++)
  185.     for (po = 21; po < 99; po++)
  186.       if (nunmap[po] >= 0)
  187.         {
  188.           ppos = nextpos+ptyp*64*64+nunmap[po]*64;
  189.           pdir = nextdir+ptyp*64*64+nunmap[po]*64;
  190.           /* dest is a function of direction and steps */
  191.           for (d = 0; d < 8; d++)
  192.             {
  193.               dest[d][0] = nunmap[po];
  194.               delta = direc[ptyp][d];
  195.               if (delta != 0)
  196.                 {
  197.                   p0 = po;
  198.                   for (s = 0; s < max_steps[ptyp]; s++)
  199.                     {
  200.                       p0 = p0 + delta;
  201.                       /*
  202.                         break if (off board) or
  203.                         (pawns only move two steps from home square)
  204.                       */
  205.                       if (nunmap[p0] < 0 || (ptyp == pawn || ptyp == bpawn)
  206.                           && s > 0 && (d > 0 || Stboard[nunmap[po]] != pawn))
  207.                         break;
  208.                       else
  209.                         dest[d][s] = nunmap[p0];
  210.                     }
  211.                 }
  212.               else
  213.                 s = 0;
  214.  
  215.               /*
  216.                 sort dest in number of steps order
  217.                 currently no sort is done due to compability with
  218.                 the move generation order in old gnu chess
  219.               */
  220.               steps[d] = s;
  221.               for (di = d; s > 0 && di > 0; di--)
  222.                 if (steps[sorted[di - 1]] == 0) /* should be: < s */
  223.                   sorted[di] = sorted[di - 1];
  224.                 else
  225.                   break;
  226.               sorted[di] = d;
  227.             }
  228.  
  229.           /*
  230.             update nextpos/nextdir,
  231.             pawns have two threads (capture and no capture)
  232.           */
  233.           p0 = nunmap[po];
  234.           if (ptyp == pawn || ptyp == bpawn)
  235.             {
  236.               for (s = 0; s < steps[0]; s++)
  237.                 {
  238.                   ppos[p0] = (unsigned char) dest[0][s];
  239.                   p0 = dest[0][s];
  240.                 }
  241.               p0 = nunmap[po];
  242.               for (d = 1; d < 3; d++)
  243.                 {
  244.                   pdir[p0] = (unsigned char) dest[d][0];
  245.                   p0 = dest[d][0];
  246.                 }
  247.             }
  248.           else
  249.             {
  250.               pdir[p0] = (unsigned char) dest[sorted[0]][0];
  251.               for (d = 0; d < 8; d++)
  252.                 for (s = 0; s < steps[sorted[d]]; s++)
  253.                   {
  254.                     ppos[p0] = (unsigned char) dest[sorted[d]][s];
  255.                     p0 = dest[sorted[d]][s];
  256.                     if (d < 7)
  257.                       pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  258.                     /* else is already initialized */
  259.                   }
  260.             }
  261.         }
  262. }
  263.  
  264. GLOBALHANDLE hGameList, hTree, hHistory, hTTable, hHashCode, hdistdata;
  265. GLOBALHANDLE htaxidata, hnextdir, hnextpos;
  266.  
  267. /* hmm.... shouldn`t main be moved to the interface routines */
  268. int
  269. init_main (HWND hWnd)
  270. {
  271.   short int ahead = true, hash = true;
  272.  
  273. /* Mods for MS WINDOWS includes moving statics to dynamic allocated */
  274.  
  275.    hHistory = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  276.                           (long) (8192 * sizeof (char) ) );
  277.  
  278.    if ( hHistory == NULL ) {
  279.       return 1;
  280.    }
  281.  
  282.    history = (unsigned char far *) GlobalLock (hHistory);
  283.    
  284.    hnextdir = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  285.                           (long) (64L*64L*8L * sizeof (char) ) );
  286.  
  287.    if ( hnextdir == NULL ) {
  288.       return 1;
  289.    }
  290.  
  291.    nextdir = (unsigned char far *) GlobalLock (hnextdir);
  292.    
  293.    hnextpos = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  294.                           (long) (64L*64L*8L * sizeof (char) ) );
  295.  
  296.    if ( hnextpos == NULL ) {
  297.       return 1;
  298.    }
  299.  
  300.    nextpos = (unsigned char far *) GlobalLock (hnextpos);
  301.  
  302.    hdistdata = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  303.                           (long) (64*64 * sizeof (short) ) );
  304.  
  305.    if ( hdistdata == NULL ) {
  306.       return 1;
  307.    }
  308.  
  309.    distdata = (short far *) GlobalLock (hdistdata);
  310.  
  311.    htaxidata = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  312.                           (long) (64*64 * sizeof (short) ) );
  313.  
  314.    if ( htaxidata == NULL ) {
  315.       return 1;
  316.    }
  317.  
  318.    taxidata = (unsigned short far *) GlobalLock (htaxidata);
  319.  
  320.    hHashCode = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  321.                           (long) (2*7*64 * sizeof (struct hashval) ) );
  322.  
  323.    if ( hHashCode == NULL ) {
  324.       return 1;
  325.    }
  326.  
  327.    hashcode = (struct hashval far *) GlobalLock (hHashCode);
  328.  
  329.    hTree = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  330.                           (long) (2000 * sizeof (struct leaf)) );
  331.  
  332.    if ( hTree == NULL ) {
  333.       return 1;
  334.    }
  335.  
  336.    Tree = (struct leaf far *) GlobalLock ( hTree);
  337.  
  338.    hGameList = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  339.                           (long) (512 * sizeof (struct GameRec)) );
  340.  
  341.    if ( hGameList == NULL ) {
  342.       return 1;
  343.    }
  344.  
  345.    GameList = (struct GameRec far *) GlobalLock (hGameList);
  346.  
  347.    hTTable = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  348.                           (long) (2*ttblsz * sizeof (struct hashentry)) );
  349.  
  350.    if ( hTTable == NULL ) {
  351.       return 1;
  352.    }
  353.  
  354.    ttable = (struct hashentry far *) GlobalLock (hTTable);
  355.  
  356.   Level = 0;
  357.   TCflag = false;
  358.   OperatorTime = 0;
  359.   Initialize ();
  360.   Initialize_dist ();
  361.   Initialize_moves ();
  362.   NewGame (hWnd);
  363.  
  364.   flag.easy = ahead;
  365.   flag.hash = hash;
  366.   hashfile = NULL;
  367.   return (0);
  368. }
  369.  
  370.  
  371. void FreeGlobals (void)
  372. {
  373.    if ( hHistory ) {
  374.       GlobalUnlock (hHistory);
  375.       GlobalFree   (hHistory);
  376.    }
  377.  
  378.    if ( hnextdir ) {
  379.       GlobalUnlock (hnextdir);
  380.       GlobalFree   (hnextdir);
  381.    }
  382.  
  383.    if ( hGameList ) {
  384.       GlobalUnlock (hGameList);
  385.       GlobalFree   (hGameList);
  386.    }
  387.  
  388.    if ( hTTable ) {
  389.       GlobalUnlock (hTTable);
  390.       GlobalFree   (hTTable);
  391.    }
  392.  
  393.    if ( hTree ) {
  394.       GlobalUnlock (hTree);
  395.       GlobalFree   (hTree);
  396.    }
  397.  
  398.    if ( hHashCode ) {
  399.       GlobalUnlock (hHashCode);
  400.       GlobalFree   (hHashCode);
  401.    }
  402.  
  403.    if ( htaxidata ) {
  404.       GlobalUnlock (htaxidata);
  405.       GlobalFree   (htaxidata);
  406.    }
  407.  
  408.    if ( hdistdata ) {
  409.       GlobalUnlock (hdistdata);
  410.       GlobalFree   (hdistdata);
  411.    }
  412.  
  413.    if ( hnextpos ) {
  414.       GlobalUnlock (hnextpos);
  415.       GlobalFree   (hnextpos);
  416.    }
  417.  
  418. }
  419.  
  420.  
  421. void
  422. NewGame (HWND hWnd)
  423.  
  424. /*
  425.   Reset the board and other variables to start a new game.
  426. */
  427.  
  428. {
  429.   short l, c, p;
  430.  
  431.   stage = stage2 = -1;          /* the game is not yet started */
  432.  
  433.   if ( flag.post ) {
  434.       SendMessage ( hStats, WM_SYSCOMMAND, SC_CLOSE, 0 );
  435.       flag.post = false;
  436.   }
  437.  
  438.   flag.mate = flag.quit = flag.reverse = flag.bothsides = false;
  439.   flag.force = false;
  440.   flag.hash = flag.easy = flag.beep = flag.rcptr = true;
  441.   NodeCnt = et0 = epsquare = 0;
  442.   dither = 0;
  443.   Awindow = 90;
  444.   Bwindow = 90;
  445.   xwndw = 90;
  446.   MaxSearchDepth = 29;
  447.   contempt = 0;
  448.   GameCnt = 0;
  449.   Game50 = 1;
  450.   hint = 0x0C14;
  451.   ZeroRPT ();
  452.   Developed[white] = Developed[black] = false;
  453.   castld[white] = castld[black] = false;
  454.   PawnThreat[0] = CptrFlag[0] = false;
  455.   Pscore[0] = 12000;
  456.   Tscore[0] = 12000;
  457.   opponent = white;
  458.   computer = black;
  459.   for (l = 0; l < 2000; l++)
  460. /*    Tree[l].f = Tree[l].t = 0;*/
  461.     (Tree+l)->f = (Tree+l)->t = 0;
  462. #if ttblsz
  463.   rehash = 6;
  464.   ZeroTTable ();
  465.   srand ((unsigned int) 1);
  466.   for (c = white; c <= black; c++)
  467.     for (p = pawn; p <= king; p++)
  468.       for (l = 0; l < 64; l++)
  469.         {
  470.           (hashcode+c*7*64+p*64+l)->key = (((unsigned long) urand ()));
  471.           (hashcode+c*7*64+p*64+l)->key += (((unsigned long) urand ()) << 16);
  472.           (hashcode+c*7*64+p*64+l)->bd = (((unsigned long) urand ()));
  473.           (hashcode+c*7*64+p*64+l)->bd += (((unsigned long) urand ()) << 16);
  474.         }
  475. #endif /* ttblsz */
  476.   for (l = 0; l < 64; l++)
  477.     {
  478.       board[l] = Stboard[l];
  479.       color[l] = Stcolor[l];
  480.       Mvboard[l] = 0;
  481.     }
  482.   if (TCflag)
  483.     SetTimeControl ();
  484.   else if (Level == 0) {
  485.     OperatorTime = 1;
  486.     TCmoves = 40;
  487.     TCminutes = 60;
  488.     TCflag = (TCmoves > 1);
  489.     SetTimeControl ();
  490.   }
  491.   InitializeStats ();
  492.   time0 = time ((long *) 0);
  493.   ElapsedTime (1);
  494.   UpdateDisplay (hWnd, 0, 0, 1, 0);
  495. }
  496.  
  497.